package com.embhealth.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.constant.RedisMessageConstant;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Order;
import com.embhealth.service.OrderService;
import com.embhealth.utils.SMSUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderControllerr {

    @Autowired
    private JedisPool jedisPool;
    @Reference
    private OrderService orderService;
    /**
     * 提交预约信息
     * @param map
     * @return
     */
    @RequestMapping("/submitOrder")
    public Result submit(@RequestBody Map map){
        //进行验证码比对，如果不成功返回信息到页面
        String telephone = (String)map.get("telephone");
        String validatecodeRedis = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        String validateCode=(String)map.get("validateCode");
        if (validateCode!=null && validatecodeRedis.equals(validateCode)){
            //标记预约方式是微信预约
            map.put("ordertype", Order.ORDERTYPE_WEIXIN);
            //调用预约业务处理
            Result result=null;
            try{
                result = orderService.order(map);
                //如果预约成功使用模板发送短信
                SMSUtils.sendShortMessage(SMSUtils.ORDER_NOTICE,map.get("telephone").toString(),map.get("orderDate").toString());
                return result;
            }catch (Exception e){
                e.printStackTrace();
                return result;
            }
        }else {
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }

    /**
     * 根据id查询预约相关信息
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Map map=orderService.findById(id);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,map);
        }catch (Exception e) {
            return new Result(false,MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /**
     * 根据日期范围和用户查询预约列表
     * @param starttime
     * @param endtime
     * @param request
     * @return
     */
    @RequestMapping("/findAllOrderByDate")
    public Result findAllOrderByDate(String starttime, String endtime, HttpServletRequest request){
        try {
            //获取用户电话
            Cookie[] cookies = request.getCookies();//获取cookie数组
            //遍历cookie
            String telephone="";
            for (Cookie c:cookies) {
                if (c.getName().equals("member_telephone")) {
                    telephone = c.getValue();
                }
            }
            List<Map<String,Object>> list = orderService.findAllOrderByDate(starttime, endtime, telephone);
            return new Result(true,"查询预约列表成功",list);
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询预约列表失败");
        }
    }

    /**
     * 根据套餐id回显
     * @param id
     * @return
     */
    @RequestMapping("findOrderById")
    public Result findOrderById(Integer id){
        try{
            //根据预约信息id封装页面映射关系
           Map<String, Object> mapByOrderId = orderService.findMapByOrderId(id);
            return new Result(true,"查询预约信息成功",mapByOrderId);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"查询预约信息失败");
        }
    }
}
